<template>
  <el-page-header content="个人中心" title="后台管理系统" icon=""></el-page-header>
  <div class="app-center">
    
    <Descrition title="">
      <template #descrition>
       Your Account
      </template>
    </Descrition>
    <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    class="center-ruleForm"
    
    status-icon
  >
    <el-form-item label="Email" prop="email">
      <el-input v-model="ruleForm.email" />
    </el-form-item>
    <el-form-item label="姓名" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        修改
      </el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import Descrition from '@/components/descrition/Descrition.vue';
import { getCenter ,setCenter} from '@/server/center'
import { ElMessage } from 'element-plus'
const router = useRouter()
interface RuleForm {
  email:string,
  userName: string
}

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  email:'',
  userName: '',
})

const rules = reactive<FormRules<RuleForm>>({
  email: [
    { required: true, message: 'Please input Activity email', trigger: 'blur' }
  ],
  userName: [
    { required: true, message: 'Please input Activity userName', trigger: 'blur' }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      setCenterData()
    } else {
      console.log('error submit!', fields)
    }
  })
}
const getCenterData=async ()=>{
  const data= await getCenter()
  ruleForm.email=data.email
  ruleForm.userName=data.userName
}
const setCenterData=async ()=>{
  try{
    await setCenter()
    ElMessage.success("修改成功")
  } catch(err){
    ElMessage.error("修改失败")
  }
}
getCenterData()

</script>
<style lang="scss" scoped>
  .app-center{
     width: 60%;
     margin:20px 0;
    .center-ruleForm{
      margin-top: 20px;
      // width: 60%;
    }
  }
</style>